package org.example.projecttwo.controller.system;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.example.projecttwo.common.Result;
import org.example.projecttwo.entity.FriendLink;
import org.example.projecttwo.entity.dto.common.BatchUpdateStatusDTO;
import org.example.projecttwo.service.IFriendLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/fk")
@Tag(name = "友情链接管理", description = "友情链接信息管理")

public class FriendLinkController {

    @Autowired
    private IFriendLinkService friendLinkService;

    @GetMapping("/list")
    @Operation(summary = "获取友情链接列表", description = "获取所有友情链接列表 -- 友情链接管理")
    public Result<List<FriendLink>> listFks(){
        return Result.success(friendLinkService.list());
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取友情链接详情", description = "根据ID获取友情链接详情 -- 友情链接管理")
    public Result<FriendLink> getFk(@PathVariable Long id){
        FriendLink fk = friendLinkService.getById(id);
        return Result.success(fk);
    }

    @PostMapping
    @Operation(summary = "新增友情链接", description = "新增友情链接，默认为不显示状态 -- 友情链接管理")
    public Result<FriendLink> createFk(@Valid @RequestBody FriendLink fk){
        friendLinkService.save(fk);
        return Result.success(fk);
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改友情链接", description = "修改友情链接信息 -- 友情链接管理")
    public Result<FriendLink> updateFk(@PathVariable Long id, @Valid @RequestBody FriendLink fk){
        fk.setId(id);
        friendLinkService.updateById(fk);
        return Result.success(fk);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除友情链接", description = "根据ID删除友情链接 -- 友情链接管理")
    public Result<String> deleteFk(@PathVariable Long id) {
        friendLinkService.removeById(id);
        return Result.success("删除成功");
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新友情链接状态", description = "更新友情链接的显示状态 -- 友情链接管理")
    public Result<String> updateFkStatus(@PathVariable Long id, @NotNull(message = "状态值不能为空")
                                        @Min(value = 0, message = "状态值必须为0或1") @Max(value = 1, message = "状态值必须为0或1")
                                        @RequestParam Integer isShow) {
        FriendLink fk = new FriendLink();
        fk.setId(id);
        fk.setIsShow(isShow);
        friendLinkService.updateById(fk);
        return Result.success("更新成功");
    }

    @PostMapping("/batch-update-status")
    @Operation(summary = "批量更新友情链接状态", description = "批量更新友情链接的显示状态 -- 友情链接管理")
    public Result<String> batchUpdateFkStatus(@Valid @RequestBody BatchUpdateStatusDTO dto){
        friendLinkService.batchUpdateStatus(dto.getIds(),dto.getIsShow());
        return Result.success("批量更新成功,共" + dto.getIds().size() + "条");
    }

    @PostMapping("/batch-delete")
    @Operation(summary = "批量删除友情链接", description = "批量删除友情链接 -- 友情链接管理")
    public Result<String> batchDeleteFk(@Valid @RequestBody @NotEmpty List<Long> ids){
        friendLinkService.removeByIds(ids);
        return Result.success("批量删除成功,共" + ids.size() + "条");
    }

    @PostMapping("/update-sort")
    @Operation(summary = "保存拖拽排序结果", description = "保存友情链接拖拽排序结果 -- 友情链接管理")
    public Result<String> updateSort(@RequestBody List<Long> fkIds){
        friendLinkService.updateSortOrder(fkIds);
        return Result.success("排序更新成功");
    }

}